package com.ziyun.pop.modules.mercharReport.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ziyun.pop.common.e.mercharReport.StoreOutEnum;
import com.ziyun.pop.common.utils.JsonUtils;
import com.ziyun.pop.modules.mercharReport.dao.ReportStoreOutDao;
import com.ziyun.pop.modules.mercharReport.entity.ReportStoreOutEntity;
import com.ziyun.pop.modules.mercharReport.entity.ReportStoreOutVO;
import com.ziyun.pop.modules.mercharReport.service.ReportStoreOutService;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;
import io.renren.common.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;


@Service("reportStoreOutService")
public class ReportStoreOutServiceImpl extends ServiceImpl<ReportStoreOutDao, ReportStoreOutEntity> implements ReportStoreOutService {

    @Autowired
    private ReportStoreOutDao reportStoreOutDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        Page<ReportStoreOutEntity> page = (Page<ReportStoreOutEntity>) this.page(
                new Query<ReportStoreOutEntity>(params).getPage(),
                new QueryWrapper<ReportStoreOutEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public R list(Map<String, Object> params) {
        Page<ReportStoreOutVO> page = new Page<>();
        page.setCurrent(Integer.valueOf(params.get("page").toString()));
        page.setSize(Integer.valueOf(params.get("limit").toString()));
        List<ReportStoreOutVO> list = reportStoreOutDao.list(page, params);
        page.setRecords(list);
        PageUtils pageUtils = new PageUtils(page);
        // 存放枚举类到userdata
        Map<String, Object> mapEnum = new HashMap<>();
        mapEnum.put("storeOutEnum", JsonUtils.enumToList(StoreOutEnum.class)); //库存交易类型
        //含税总金额
        List<ReportStoreOutVO> listAll = reportStoreOutDao.list(params);
//        Optional<BigDecimal> optionalBigDecimal = listAll.stream().map(ReportStoreOutVO::getTotalMoney).reduce(BigDecimal::add);
        BigDecimal totalMoney = listAll.stream().map(ReportStoreOutVO::getTotalMoney).reduce(new BigDecimal("0"), BigDecimal::add);
        mapEnum.put("totalMoney", totalMoney);
        return R.ok().put("page", pageUtils).put("userData", mapEnum);
    }

    @Override
    public List<ReportStoreOutVO> exportExcel(Map<String, Object> params) {
        return reportStoreOutDao.list(params);
    }

}
